package com.xiaoshuidi.cloud.module.member.service.invite;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.framework.mybatis.core.util.MyBatisUtils;
import com.xiaoshuidi.cloud.module.member.controller.admin.invite.vo.InviteActivityConfigVo;
import com.xiaoshuidi.cloud.module.member.controller.admin.invite.vo.InviteActivityLogVo;
import com.xiaoshuidi.cloud.module.member.controller.admin.invite.vo.InviteLogPageVo;
import com.xiaoshuidi.cloud.module.member.controller.app.invite.vo.AddInviteLogVo;
import com.xiaoshuidi.cloud.module.member.controller.app.invite.vo.InviteSummaryVo;
import com.xiaoshuidi.cloud.module.member.enums.InviteActivityLogStatusEnum;
import com.xiaoshuidi.cloud.module.member.enums.point.PointRecordEnums;
import com.xiaoshuidi.cloud.module.member.mapper.invite.InviteActivityLogMapper;
import com.xiaoshuidi.cloud.module.member.pojo.invite.InviteActivityLogEntity;
import com.xiaoshuidi.cloud.module.member.service.point.MemberPointsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/***
 * @description 邀请活动service层
 * @author Leo.Li
 * @date 2023-05-19 17:20:47
 * @version generator
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class InviteActivityLogService {

    @Autowired
    InviteActivityLogMapper inviteActivityLogMapper;
    @Resource
    InviteActivityConfigService inviteActivityConfigService;

    private final MemberPointsService memberPointsService;

    public PageResult<InviteActivityLogVo> getInviteActivityLogPage(InviteLogPageVo form) {
        IPage<InviteActivityLogVo> page = MyBatisUtils.buildPage(form);
        List<InviteActivityLogVo> list = inviteActivityLogMapper.selectInviteActivityLogPage(page, form);
        for (InviteActivityLogVo vo : list) {
            InviteSummaryVo temp = this.getInviteSummary(vo.getAccountId());
            vo.setRewardCount(temp.getCount());
            if (temp.getTotal() != null) {
                vo.setRewardSummary(temp.getTotal());
            } else {
                vo.setRewardSummary(0);
            }
        }
        PageResult<InviteActivityLogVo> pageResult = new PageResult(list, page.getTotal());
        pageResult.setList(list);
        return pageResult;
    }

    public InviteSummaryVo getInviteSummary(Long tenantUserId) {
        return inviteActivityLogMapper.getUserInviteSummary(tenantUserId);
    }

    public PageResult<InviteActivityLogVo> getUserInviteActivityLogs(InviteLogPageVo form) {
        IPage<InviteActivityLogVo> page = MyBatisUtils.buildPage(form);
        List<InviteActivityLogVo> list = inviteActivityLogMapper.getUserInviteActivityLogs(page, form);
        PageResult<InviteActivityLogVo> pageResult = new PageResult(list, page.getTotal());
        pageResult.setList(list);
        return pageResult;
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void addUserInviteLog(AddInviteLogVo form) {
        Long count = inviteActivityLogMapper.selectCount(new QueryWrapper<InviteActivityLogEntity>().eq("invite_phone", form.getInvitePhone()));
        if (count > 0) {
            log.error("该用户已经被邀请过了, invite_phone: {}", form.getInvitePhone());
            return;
        }
        InviteActivityLogEntity activityLog = new InviteActivityLogEntity();
        InviteActivityConfigVo config = inviteActivityConfigService.getValidInviteConfig();
        if (config == null) {
            log.error("未配置邀请奖励");
            return;
        }
        activityLog.setStatus(InviteActivityLogStatusEnum.TAKE_EFFICACY.getCode());
        activityLog.setConfigId(config.getId());
        activityLog.setRewardPoint(config.getReward());
        activityLog.setInvitePoint(config.getInvite());
        activityLog.setAccountId(form.getAccountId());
        activityLog.setInviteAccountId(form.getInviteAccountId());
        activityLog.setInvitePhone(form.getInvitePhone());
        activityLog.setCreator(form.getInviteAccountId().toString());
        inviteActivityLogMapper.insert(activityLog);
        //老租客新增积分
        memberPointsService.memberPointsChange(form.getAccountId(), config.getReward(), PointRecordEnums.INVITE_NEW_CUSTOMERS, "邀请有礼奖励积分");
        //新租客新增积分
        memberPointsService.memberPointsChange(form.getInviteAccountId(), config.getInvite(), PointRecordEnums.INVITE_NEW_CUSTOMERS, "邀请有礼奖励积分");
    }

    public void updateUserId(Long oldUserId, Long userId, String mobile) {
//        //修改邀请租客id
//        inviteActivityLogMapper.updateAccountId(oldUserId,userId);
        //修改被邀请
        inviteActivityLogMapper.updateInviteAcount(oldUserId,userId,mobile);
    }
}

